package com.hey.baseapplication.presenter;


import com.hey.baseapplication.model.BaseModel;
import com.hey.baseapplication.view.BaseView;

import rx.Subscription;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by Hey on 16/12/15.
 */

public abstract class BasePresenter<T extends BaseView,M extends BaseModel> {
    //一个Subscription的集合,可以同时取消订阅
    protected CompositeSubscription mSubscription;
    public T mView;
    public M mModel;
    public void destroy(){
        mView=null;
        onUnSubscribe();
    }

    //RxJava取消注册，以避免内存泄露
    public void onUnSubscribe() {
        if (mSubscription != null && mSubscription.hasSubscriptions()) {
            mSubscription.unsubscribe();
        }
    }

    //RxJava注册
    public void addSubscription(Subscription subscriber) {
        if (mSubscription == null) {
            mSubscription = new CompositeSubscription();
        }
        mSubscription.add(subscriber);
    }


}
